<?php
	/**
	 * 
	 * Type model
	 * @author Vu Tran
	 *
	 */
	class CategoryModel extends Model
	{
		/**
		 * Constructor
		 */
		function __construct()
		{
			parent::__construct();
		}
		
		/**
		 * 
		 * Gets all record from table Type
		 */
		function getAllCategories()
		{
			$sql = "select *
			from category as c
			inner join category_product as cp on c.id = cp.category_id
			inner join product as p on p.id = cp.product_id
			inner join store as s on s.id = p.store_id
			where s.status = 1";
			return $this->execute_query($sql);
		}
		
		/**
		 * 
		 * Gets category base on type
		 * @param string $query
		 */
		function getCategoriesByQuery($query)
		{
			$sql = "select distinct c.* from category as c
			inner join category_product as cp on c.id = cp.category_id 
			inner join product as p on p.id = cp.product_id
			inner join store as s on s.id = p.store_id
			where s.status = 1 and c.parent is not null " . $query;
			return $this->execute_query($sql);
		}
		
		/**
		 * 
		 * Gets a specific type by id
		 * @param string $id
		 */
		function getCategoryById($id){
			$sql = "select * from category where id = ?";
			$values = array();
			$values[0] = $id;
			$types = array();
			$types[0] = "integer";
            
			$res = $this->execute_query($sql, $values, $types);
            if(is_array($res) && count($res) > 0){
                return $res[0];
            }
            return null;
		}
    }
?>